package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/8 14:28
 * @description
 */
public class Code637 {
    private List<Double> result = new ArrayList<>();
    private List<List<Long>> temp = new ArrayList<>();

    public List<Double> averageOfLevels(TreeNode root) {
        getNode(root, 0);
        for (List<Long> r : temp) {
            long sum = r.stream().mapToLong(x -> x).sum();
            result.add(sum / (double)r.size());
        }
        return result;
    }

    private void getNode(TreeNode node, int level) {
        if (node != null) {
            if (level >= temp.size()) {
                temp.add(new ArrayList<>());
            }
            temp.get(level).add((long)node.val);
            level++;
            getNode(node.left, level);
            getNode(node.right, level);
        }
    }
}
